<template>
  <div class="goods-list">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 搜索框+搜索按钮 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button
              slot="append"
              icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="4">
          <el-button
            type="primary"
            @click="pathToAddGoods">添加商品</el-button>
        </el-col>
        <!-- 商品table部分 -->
      </el-row>
      <el-table
        :data="goodsList"
        border
        stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="95px" label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column width="70px" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="160px" label="创建时间" v-slot="data">
          {{ data.row.add_time | dateFormat }}
        </el-table-column>
        <el-table-column width="140px" label="操作" v-slot="data">
          <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="removeGoods(data.row)"></el-button>
          <p v-if="false">{{ data }}</p>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodsList',
  data () {
    return {
      // 搜索参数
      queryInfo: {
        query: '',
        pagesize: '10',
        pagenum: '1',
        // 总数
        total: null
      },
      // 存放商品列表项
      goodsList: []
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 1. 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('/goods', {
        params: {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize
        }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
    },
    // 2. 删除商品
    async removeGoods (value) {
      console.log(value)
      const result = await this.$confirm('该操作会永久删除该商品, 要继续吗', '提示', {
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') {
        return this.$message.info('已取消该操作')
      }
      const { data: res } = await this.$http.delete(`goods/${value.goods_id}`)
      if (res.meta.status !== 200) {
        this.$message.error('删除商品失败')
      }
      // 删除成功
      this.$message.success('删除商品成功!')
      // 刷新数据
      this.getGoodsList()
    },
    // 3. 跳转到添加商品页面
    pathToAddGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  > .el-breadcrumb {
    font-size: 12px;
  }
  > .el-card {
    margin-top: 15px;
    min-width: 1170px;
    .el-table {
      margin-top: 15px;
    }
  }
}
.el-table__header-wrapper {
  font-size: 12px!important;
}
</style>

<style lang="less">
.el-table__header-wrapper {
  font-size: 12px !important;
}
</style>
